package com.aaa.controller;

import com.aaa.pojo.Change;
import com.aaa.pojo.Invest;
import com.aaa.service.ChangeService;
import com.aaa.service.InvestService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping( name = "零钱理财",value = "/change")
public class ChangeController {
    @Autowired
    private ChangeService service;
    @Autowired
    private InvestService invest;
    @RequestMapping("/change")
    public String change(){
        return
                "views/change";
    }
    /**
     * 分页请求
     * @param currentPage 当前页
     * @param pageSize  每页显示大小
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserList/{currentPage}/{pageSize}",method = RequestMethod.GET)
    public PageInfo<Change> getUserByUserPage(@PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize){
        return service.getAllUsersByPage(currentPage,pageSize);
    }


    @RequestMapping(name = "添加",value = "/insert")
    public String insert(String c_name, double c_yield, BigDecimal c_earnings,BigDecimal c_amount,Integer h_id){
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = df.format(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_WEEK, h_id);
        String format1 = df.format(calendar.getTime());
        String s = format + "~" + format1;
        //添加
        invest.insertInvest(new Invest(s, h_id + ""));
        //查询
        Invest invest1=invest.selectDate(s);
        Integer h_id1 = invest1.h_id;
        //添加
        service.insertChange(new Change(c_name,c_yield,c_earnings,c_amount,h_id1));
        return "redirect:change";
    }


    @RequestMapping(name = "根据id查询",value = "/selectById")
    @ResponseBody
    public Map<String,Object> selectById(Integer c_id){
        System.out.println(c_id+"=====aaaaa");
        Map<String,Object> map = new HashMap<>();
        map.put("byId",service.selectById(c_id));
        return  map;
    }



    @RequestMapping(name = "修改",value = "/update")
    public String update(String c_name, double c_yield, BigDecimal c_earnings, BigDecimal c_amount, Integer h_id,Integer c_id){
        System.out.println(c_id+"======aaaaa");
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = df.format(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_WEEK, h_id);
        String format1 = df.format(calendar.getTime());
        String s = format + "~" + format1;
        service.updateChange(new Change(c_name, c_yield, c_earnings, c_amount, s, h_id+"", c_id));
        return "redirect:change";
    }
    @RequestMapping(name = "删除",value = "/delete")
    @ResponseBody
    public PageInfo<Change> delete(Integer c_id){
        System.out.println(c_id+"=====aaaaa");
        boolean b = service.deleteChange(c_id);
         PageInfo<Change> allUsersByPage = service.getAllUsersByPage(0, 5);
        return allUsersByPage;
    }
}
